﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Model;
using BLL;
using Newtonsoft.Json;
namespace LandAppraisal.Controllers
{
    public class UsersController : ApiController
    {
        AdminBLL bll = new AdminBLL();

        #region 获取员工信息表
        /// <summary>
        /// 获取员工信息表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IEnumerable<SysUser> Get(string name)
        {
            if (name != null)
            {
                return bll.GetList<SysUser>().Where(p => p.Users_Name.Contains(name));
            }
            else
            {
                return bll.GetList<SysUser>();
            }
        }
        #endregion

        #region 添加员工信息
        /// <summary>
        /// 添加员工信息
        /// </summary>
        /// <param name="value"></param>
        [HttpPost]
        public int Post([FromBody]SysUser user)
        {
            return bll.Add<SysUser>(user);
        }
        #endregion

        #region 修改员工信息
        /// <summary>
        /// 修改员工信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="value"></param>
        [HttpPut]
        public bool Put([FromBody] SysUser user)
        {
            return bll.Update<SysUser>(user);
        }
        #endregion

        #region 修改状态
        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="id"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public int UpdateState(int id, int state)
        {
            return bll.UpdateUsersState(id, state);
        }
        #endregion

        #region 删除员工信息
        /// <summary>
        /// 删除员工信息
        /// </summary>
        /// <param name="id"></param>
        [HttpDelete]
        public bool Delete(int id)
        {
            return bll.DeleteById<SysUser>(id);
        }
        #endregion

        #region 根据部门编号获取该部门下的员工
        /// <summary>
        ///根据部门编号获取该部门下的员工
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public string GetUserListByDeptID(int id)
        {
            var list = bll.GetList<SysUser>().Where(p => p.Roles_id == 3).Where(o => o.Department_Id == id);
            return JsonConvert.SerializeObject(list);
        }
        #endregion

    }
}
